package com.hxc.controller;

import com.github.pagehelper.PageInfo;
import com.hxc.pojo.Cartiy;
import com.hxc.pojo.News;
import com.hxc.pojo.NewsExt;
import com.hxc.service.CartigyService;
import com.hxc.service.NewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
@RequestMapping("news")
public class NewsController {

    @Autowired
    private NewsService newsService;
    @Autowired
    private CartigyService cartigyService;

    @RequestMapping("findAll")
    public String findAll(Model model){
        List<News> news=newsService.findAll();
        List<Cartiy> cartiys=cartigyService.findAll();
        model.addAttribute("news",news);
        model.addAttribute("cartiys",cartiys);
        return "/WEB-INF/jsp/newsList.jsp";
    }

    @RequestMapping("deleteAll")
    public String delteAll(NewsExt newsExt){
        List<Object> list=newsExt.getCkOne();
        newsService.deleteAll(list);
        return "/news/findPage.html";
    }

    private Integer PAGE_SIZE=3;
    @RequestMapping("findPage")
    public String findPage(Model model, NewsExt newsExt, @RequestParam(defaultValue = "1") Integer pageNum){
        PageInfo<NewsExt> pageInfo =newsService.getNewsList(newsExt,pageNum,PAGE_SIZE);
        List<Cartiy> cartiys=cartigyService.findAll();
        List<News> news=newsService.findAll();
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("cartiys",cartiys);
        return "/WEB-INF/jsp/newsList.jsp";
    }

    @RequestMapping("addStr")
    public String addStr(Model model){
        List<Cartiy> cartiys=cartigyService.findAll();
        model.addAttribute("cartiys",cartiys);
        return "/WEB-INF/jsp/addNewList.jsp";
    }

    @RequestMapping("add")
    public String addNews(){
        return "";
    }

}
